/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.util;

import java.io.InputStream;


/**
 * Utility class for class loader operations
 * 
 * @author Laurent Guerin
 * @since 1.2.0
 *
 */
public class ClassLoaderUtil 
{
    /**
     * No constructor for this class !
     */
    private ClassLoaderUtil()
    {
    }

    //-----------------------------------------------------------------------------------
    /**
     * Returns the ClassLoader used by default by the framework for loading classes and resources 
     * @return the default ClassLoader
     */
    public static ClassLoader getDefaultClassLoader() 
    {
    	//--- Strategy 1 : The ClassLoader of the current thread
    	ClassLoader classLoader  = Thread.currentThread().getContextClassLoader() ;
    	
    	//--- Strategy 2 : The ClassLoader of this class
    	// ClassLoader classLoader = ClassLoaderUtil.getClassLoader() ;
    	
    	return classLoader ;
    }

    //-----------------------------------------------------------------------------------
	/**
	 * Uses the given class loader to find a resource and returns its InputStream
	 * The ClassPath is used to search the resource
	 * 
	 * @param sResource the resource name
	 * @param classLoader the class loader to be used
	 * @return the resource InputStream (or null if not found)
	 */
	public static InputStream getResourceAsStream ( String sResource, ClassLoader classLoader ) 
    {
		if ( null == sResource ) {
			throw new IllegalArgumentException("Resource name is null");
		}
		if ( null == classLoader ) {
			throw new IllegalArgumentException("Class loader is null");
		}
		
    	return classLoader.getResourceAsStream(sResource);
    }

	//-----------------------------------------------------------------------------------
	/**
	 * Uses the default class loader to find a resource and returns its InputStream<br>
	 * The ClassPath is used to search the resource
	 * 
	 * @param sResource the resource name
	 * @return the resource InputStream (or null if not found)
	 */
	public static InputStream getResourceAsStream ( String sResource )
    {
        return getResourceAsStream(sResource, getDefaultClassLoader() );
    }

	//-----------------------------------------------------------------------------------
	/**
	 * Uses the given class loader to find and load a class
	 * 
	 * @param sClassName the class name
	 * @param classLoader the class loader to be used
	 * @return the class loaded (or null if not found)
	 */
	public static Class<?> loadClass ( String sClassName, ClassLoader classLoader ) 
    {
		if ( null == sClassName ) {
			throw new IllegalArgumentException("Class name is null");
		}
		if ( null == classLoader ) {
			throw new IllegalArgumentException("Class loader is null");
		}
		
    	Class<?> c;
		try {
			c = classLoader.loadClass(sClassName);
		} catch (ClassNotFoundException e) {
        	return null ;
		}
    	return c ;
    }
	
	//-----------------------------------------------------------------------------------
	/**
	 * Uses the default class loader to find and load a class
	 * 
	 * @param sClassName the class name
	 * @return the class loaded (or null if not found)
	 */
	public static Class<?> loadClass ( String sClassName ) 
    {
		return loadClass(sClassName, getDefaultClassLoader() ) ;
    }
}
